November 15, 2018
require(RCurl)
script <- getURL("https://raw.githubusercontent.com/darwinanddavis")
eval(parse(text = script))
POORGOODp <- rep(rnorm(100),20) # this is well annotated code
cd change working dir. cd .. move one level up
pwd print current working dir
ls list files in working dir
mkdir newfolder make new working dir
touch text.txt create new file
cp source destination
copy files from source to destination. e.g. cp /Users/mydir/README.txt ~/Documents
cp -R source destination
copy all folders, subfolders, and files from source to destination
mv source destination
move files or folders from source to destination (no need for -R)
cp ~/Desktop/*.rtf ~/Documents
move multiple files with the * wildcard, which copies all .rtf files. The tilde (~) symbol is a shortcut for your Home folder, which contains '/Desktop'.
mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtfcp ~/Desktop/MyFile.rtf ~/Documents/MyFile-old.rtf
rename files
add and commit your filespush your filesOpen Terminal/cmd
cd ~/Documents/ # change working dir ls # list dir contents
Open Finder/Windows. Make a new project on your local comp.
# create new project cd ~/Documents # create new file touch test.txt open test.txt # make a new folder mkdir newgit # navigate to that folder cd newgit ls -a
Create a new file in the command line
# navigate to your new git repo pwd cd ~/Documents/newgit # move the new file into the git repo mv ~/Documents/test.txt ~/Documents/newgit ls
Initialise your new local repo
# init git git init
Add the files in your folder to the local git repo
# add the files to the git git add . # the '.' adds everything git add test.txt # adds individual files git status # check what git is doing
Stage the files for the commit
# add the files to the git git commit -m 'init commit' # -m adds a message
Let's check the changes
git log # recent git activity
Now we push the changes we made from our local repo to our Github cloud.
First, copy the Github repo link you want to push to. Select either https or SSH (requires key access).
Then push your staged (commit) files from your local repo to the remote repo
# set the new remote repo git remote set-url origin "your github repo" git remote add github "your github repo" # see what remote repo you have git remote -v # push changes from local repo to remote repo git push -u github master
fatal: remote origin already exists
The remote origin already exists, so you can't add it again
git remote rm origin # if origin already exists, remove it git remote add origin "your github repo" # then re-add git push origin master # then push again
! [rejected] master -> master (non-fast-forward) Someone else has made changes since your latest ones and git refuses to lose the commit, so won't push your new changes
git pull origin master # fetches any updates to online repo and merge them
fatal: refusing to merge unrelated histories
git pull origin master --allow-unrelated-histories # unnecessary parallel history merged to your project, # usually associated with a README.md file
This creates a git repository on your local machine complete with version control.
Every version of every file for the history of the project is grabbed by default when you run git clone.
git clone "github url" "new repo name (optional)" # e.g. git clone https://github.com/darwinanddavis/UsefulCode mynewrepo
git add . # adds all files to git. replace '.' with filename for individ files git commit -m 'redo intro' # '-m' = message
# after the above steps ^ git remote set-url origin "link to existing github repo" # talk to github git remote -v # verify the remote repo (optional) git pull origin master # pull the last commit to match your local changes git push -u origin master # push changes from local repo to remote repo git log # check recent activity. this is where you retrieve gits, e.g. '0dg45' git status # check git status at any time
Re-do a commit
git reset --soft HEAD~1
After pushing to your remote repo and this error appears:! [rejected] master -> master (fetch first)
git fetch origin master # match the local repo commit status to the push destination git merge master # git push -u origin master # for non-fast-forward error git fetch origin master:tmp git rebase tmp git push origin HEAD:master git branch -D tmp git push -u origin master
For fatal: refusing to merge unrelated histories error
git checkout master git merge origin/master --allow-unrelated-histories # or run this before your 'git pull origin master' command git pull --allow-unrelated-histories origin master
If Github questions your user credentials.
git config --global user.email "<your email>" git config --global user.name "<your github user name>"
When using SSH for your github remote repo, e.g. git@github.com:username/reponame.git
Accessing your SSH key:
- In Mac, in Terminal, type
cat ~/.ssh/id_rsa.pub
ls ~/.ssh/*.pub
How to access recent commits to your local repo
git log # check recent activity and select commit e.g. 0df4g3 ... git checkout "your commit" git checkout master # return to current branch
I'd rather you move towards open-access and reproducible research than be deterred by the git user experience.
How to access recent commits to your local repo
Origin master - rejected (fetch first), no file in GitHub repository